package top.lshaci.learning.springboot.rabbitmq.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * RabbitOrderConfig
 *
 * @author lshaci
 * @since 1.0.0
 */
@Configuration
public class RabbitOrderConfig {

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange("order-event-exchange");
    }

    /**
     * x-dead-letter-exchange:	    order-event-exchange
     * x-dead-letter-routing-key:	order.release.order
     * x-message-ttl:	            60000
     */
    @Bean
    public Queue orderDelayQueue() {
        Map<String, Object> arguments = new HashMap<>(4);
        arguments.put("x-message-ttl", 60_000);
        arguments.put("x-dead-letter-exchange", "order-event-exchange");
        arguments.put("x-dead-letter-routing-key", "order.release.order");
        return new Queue("order.delay.queue", true, false, false, arguments);
    }

    @Bean
    public Queue orderReleaseOrderQueue() {
        return new Queue("order.release.order.queue");
    }

    @Bean
    public Binding orderCreateOrderBinding() {
        return new Binding(
                "order.delay.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.create.order", null
        );

    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(
                "order.release.order.queue", Binding.DestinationType.QUEUE,
                "order-event-exchange", "order.release.order", null
        );
    }
}
